package chzhenjia.learn.androidcode;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.List;

import chzhenjia.learn.androidcode.config.ApplicationConfig;

/**
 *
 * @author 陈圳佳
 *         日期:2015/7/11
 */
public class MainActivity extends AppCompatActivity {

    private List<String> list;
    private BaseAdapter _mAdapter = new BaseAdapter() {
        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View item = convertView == null ? View.inflate(MainActivity.this, R.layout.main_item, null) : convertView;
            TextView tx = (TextView) item.findViewById(R.id.main_item_tx);
            tx.setText(list.get(position));
            return item;
        }
    };
    private AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Class<?> activity = ApplicationConfig.getActivity(list.get(position));
            Intent intent = new Intent(MainActivity.this, activity);
            startActivity(intent);
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        list = ApplicationConfig.getClassList();
        ListView mainLv = (ListView) findViewById(R.id.main_lv);
        mainLv.setAdapter(_mAdapter);
        mainLv.setOnItemClickListener(itemClickListener);
    }

}
